function User() {
    this.table = this.InitTable();
    this.tab = this.InitTab();
    this.form = this.InitEleUI();
    this.InitOrg();
}


User.prototype.InitOrg = function () {
    var comboboxm = $("#orgid").ligerComboBox({
        valueField: 'orgid',
        textField: 'orgname',
        selectBoxWidth: 250,
        selectBoxHeight: 250,
        treeLeafOnly: false,
        valueFieldID: "hid",
        tree: {
            url: '../common/get_org_list?orgtype=0',
            checkbox: false,
            nodeWidth: 150,
            ajaxType: 'get',
            idFieldName: 'orgid',
            textFieldName: "orgname",
            idField: "orgid",
            parentIDField: "orgparentid"
        },
        onSelected: function (orgid, orgname) {// id为空值，name为选择的值
            $("#hidszdw").val(orgid);
            vUser.searchtable();
        },
        width: 250
    });
    return comboboxm;
}

User.prototype.InitEleUI = function () {
    var form;
    layui.use('form', function () {
        form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功

        form.on('select(zzmm)', function (data) {
            $("#hidzzmm").val(data.value);
            form.render('select');
            vUser.searchtable();
        });

        form.on('select(szdw)', function (data) {
            $("#hidszdw").val(data.value);
            form.render('select');
        });

        form.render();
    });
    return form;
}
User.prototype.InitTab = function () {
    var element;
    layui.use('element', function () {
        element = layui.element;
    });
    return element;
}

User.prototype.OpTab = function (tabindex, obj) {
    this.tab.tabChange('tabcontent', tabindex);
    if (obj) {
        document.formedit.rolename.value = obj.rolename;
        document.formedit.remark.value = obj.remark;
        document.formedit.roleid.value = obj.roleid;
        $("input[name='roletype']").removeAttr("checked");
        $("input[name='roletype']").eq(obj.roletype).attr("checked", "");
        this.form.render();
    }
}

User.prototype.InitTable = function () {
    var table;
    var _this = this;
    layui.use('table', function () {
        table = layui.table;
        table.render({
            elem: '#list'
            , height: $(window).height() - 140
            , url: 'get_list' // 数据接口
            , page: true // 开启分页
            , limit: 40
            , id: 'userlist'
            , method: "post"
            , limits: [20, 40, 80, 100]
            , cols: [[ // 表头
                //{type:'numbers'}
                // {field : 'rowNum',title : '序号',width : 80,align : 'center'},
                {field: 'realname', title: '姓名', width: 170, align: 'center', fixed: 'left'}
                , {field: 'orgname', title: '实际工作单位', width: 170, align: 'center'}
                , {field: 'rsgxorgname', title: '人事关系所在单位', width: 170, align: 'center'}

                , {field: 'cardno', title: '身份证号', width: 180, align: 'center'}
                , {field: 'sex', title: '性别', width: 80, align: 'center'}
                //,{field : 'username',title : '用户名',width : 180,align : 'center'}
                , {field: 'ldzw', title: '职务', width: 180, align: 'center'}
                , {field: 'rolename', title: '角色', width: 180, align: 'center'}
                //, {field : 'status',title : '状态',width : 80,align : 'center'}

                // , {field : 'realname',title : '姓名',width : 100,align : 'center'}

                , {field: 'nation', title: '民族', width: 80, align: 'center'}
                , {field: 'userjg', title: '籍贯', width: 120, align: 'center'}
                , {field: 'birthplace', title: '出生地', width: 150, align: 'center'}
                , {field: 'domicileplace', title: '户籍地', width: 150, align: 'center'}
                , {field: 'zzmm', title: '政治面貌', width: 100, align: 'center'}
                , {field: 'birthday', title: '出生日期', width: 170, align: 'center'}

                , {field: 'userno', title: '警号', width: 150, align: 'center'}
                , {field: 'cjgzsj', title: '参加工作时间', width: 170, align: 'center'}
                , {field: 'cjgagzsj', title: '参加公安工作时间', width: 170, align: 'center'}
                , {field: 'qualifications', title: '全日制学历', width: 150, align: 'center'}
                , {field: 'degree', title: '全日制学位', width: 150, align: 'center'}
                , {field: 'zzjyxl', title: '最高学历', width: 120, align: 'center'}
                , {field: 'zzjyxw', title: '最高学位', width: 120, align: 'center'}
                , {field: 'phone', title: '电话', width: 130, align: 'center'}
                , {field: 'createtime', title: '时间', width: 170, align: 'center'}
                , {field: 'userstatus', title: '用户状态', width: 200, align: 'center'}
                , {field: 'rank', title: '职级', width: 200, align: 'center'}
                , {field: 'joinpartytime', title: '入党时间', width: 200, align: 'center'}
                , {field: 'remark', title: '备注', width: 200, align: 'center'}
                , {title: '操作', width: 100, align: 'center', toolbar: '#operate'}
            ]]
            , request: {
                type: 'post'
            }
            , even: true //开启隔行背景
            , where: {
                realname: $("#realname").val(),
                userno: $("#userno").val(),
                zzmm: $("#hidzzmm").val(),
                szdw: $("#hidszdw").val()
            }
            , done: function (res, page, count) {
                var dataarr = res.data;


                $("[data-field='sex']").children().each(function (index) {
                    var text = $(this).text();
                    if (text == "0") {
                        $(this).text('男')
                    }
                    else if (text == "1") {
                        $(this).text('女')
                    }
                })

                $("[data-field='status']").children().each(function (index) {
                    var text = $(this).text();
                    if (text == "0") {
                        $(this).text('正常')
                    }
                    else if (text == "1") {
                        $(this).text('停用')
                    }
                })

                $("[data-field='rank']").children().each(function (index) {
                    var text = $(this).text();
                    var html = "";

                    switch (text) {

                        case "1":
                            html = '处级领导干部';

                            break;
                        case  "2":
                            html = '科级领导干部';
                            break;
                        case "3":
                            html = '民警';
                            break;
                        case  "4":
                            html = '未定职务(新录用公务员)';
                            break;
                        case "5":
                            html = '领导班子';
                            break;
                        default:
                            html = text;
                            break;

                    }

                    $(this).text(html)
                })

            }

        });


        //监听工具条
        table.on('tool(list)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'del') {

                top.mylayui.confirm('确定要删除吗?删除会关联删除该客户的所有信息', {icon: 0}, function (index, dd) {
                    top.mylayui.close(index);
                    vUser.deleteentity(data.userid);
                });
            } else if (obj.event === 'edit') {
                //layer.alert('编辑行：<br>'+ JSON.stringify(data))
                //_this.OpTab(2,data);
                vUser.openadd(obj.data.userid);
            }
        });


    });

    return table;
}

User.prototype.searchtable = function () {
    this.table.reload('userlist', {
        page: {
            curr: 1 //重新从第 1 页开始
        }
        , where: {
            realname: $("#realname").val(),
            userno: $("#userno").val(),
            zzmm: $("#hidzzmm").val(),
            szdw: $("#hidszdw").val()
        }
    });
}


User.prototype.deleteentity = function (userid) {
    $.ajax({
        type: "POST",
        url: "delete_user",
        data: {userid: userid},
        beforeSend: function (XHR) {

        },
        success: function (data) {
            data = eval('(' + data + ')');
            if (data.result > 0) {
                top.mylayui.msg(data.msg, {icon: 1});
                //vOrg.searchtable();
                document.location.reload();
            }
            else {
                top.mylayui.msg(data.msg, {icon: 0});
            }
        },
        complete: function (XHR) {

        },
        error: function () {

        }
    });
}

User.prototype.openadd = function (userid) {
    top.mylayui.open({
        type: 2,
        title: '用户添加',
        shadeClose: true,
        shade: 0.5,
        area: ['870px', '550px'],
        content: 'user/user_edit?userid=' + userid
    });

    var iframeele = top.$(".layui-layer-content").find('iframe');
    iframeele.css({"height": $(iframeele).height() - 10});
}

User.prototype.addresult = function (result, msg) {
    if (result > 0) {
        top.mylayui.msg('操作成功', {icon: 1});
        top.mylayui.closeAll('iframe');
        vUser.searchtable();
        if (msg > 0) {
            top.mylayui.msg('考勤生成成功', {icon: 1});

        }
    }
    else {
        if (msg) {
            top.mylayui.msg(msg, {icon: 0});
        } else {
            top.mylayui.msg('操作失败', {icon: 0});
        }
    }
}

var vUser;
$(function () {
    vUser = new User();
})


function CloseDialog(result, msg) {

    if (result > 0) {
        top.mylayui.msg('操作成功', {icon: 1});
        document.formedit.reset();
        //vOrg.searchtable();
        //vOrg.OpTab(1);
        document.location.reload();
    }
    else {
        if (msg) {
            top.mylayui.msg(msg, {icon: 0});
        } else {
            top.mylayui.msg('操作失败', {icon: 0});
        }
    }
}

User.prototype.importTab = function () {
    top.mylayui.open({
        type: 2,
        title: "用户信息导入",
        shadeClose: true,
        shade: 0.5,
        area: ['400px', '200px'],
        content: 'user/user_list_info_import'
    });
}


User.prototype.exportExcel = function () {
    $.ajax({
        type: "POST",
        url: "export",
        data: {

            realname: $("#realname").val(),
            userno: $("#userno").val(),
            zzmm: $("#hidzzmm").val(),
            szdw: $("#hidszdw").val()


        },
        beforeSend: function (XHR) {
            top.ShowZheZhao();
        },
        success: function (data) {

            data = eval('(' + data + ')');
            if (data.code == 200) {
                //top.mylayui.msg(data.msg, {icon: 1});
                //alert(data.msg);
                window.open(data.msg, '_self');
            }
            else {
                layui.msg(data.msg, {icon: 0});
            }

        },
        complete: function (XHR) {
            top.ShowZheZhao();
        },
        error: function () {

        }
    });
}
